var tick = '../Sign/image/tick.png';
var cancel = '../Sign/image/cancel.png';
var asterisk_orange = '../Sign/image/asterisk_orange.png';

$(document).ready(function(){
    validate.name($("input[name=signer_name]"));
    validate.phone($("input[name=signer_phone]"));
    validate.wechat($("input[name=signer_wechat]"));
    
    // validate.email($("input[name=visitor_email]"));
    validate.idcard($("input[name=signer_idcart]"));
    validate.unit($("input[name=signer_unit]"));
    validate.address($("input[name=signer_address]"));
    
});

$(function(){
    $('input[name=signer_name]').bind('input oninput', function(){
        validate.name($("input[name=signer_name]"));
    });
    $('input[name=signer_phone]').bind('input oninput', function(){
        validate.phone($("input[name=signer_phone]"));
    });
    $('input[name=signer_idcart]').bind('input oninput', function(){
        validate.idcard($("input[name=signer_idcart]"));
    });
    $('input[name=signer_wechat]').bind('input oninput', function(){
        validate.wechat($("input[name=signer_wechat]"));
    });
    $('input[name=signer_unit]').bind('input oninput', function(){
        validate.unit($("input[name=signer_unit]"));
    });
    $('input[name=signer_address]').bind('input oninput', function(){
        validate.address($("input[name=signer_address]"));
    });


})

var validate = {
    search_str: String(),
    flag: Array(),
    name: function(visitor_name){
        this.search_str = /^[\u4e00-\u9fa5]*$/;
        var visitor_name_val = visitor_name.val();
        if (visitor_name_val != '' && this.search_str.test(visitor_name_val) && visitor_name_val.indexOf("小姐") == -1 && visitor_name_val.indexOf("先生") == -1 && visitor_name_val.indexOf("同学") == -1) {
            this.flag[0] = true;
            $("#signer_name_i").css('background','url("'+ tick +'") no-repeat 100% 7px');
            console.log('signer_name true');
        }else{
            this.flag[0] = false;
            $("#signer_name_i").css('background','url("'+ cancel +'") no-repeat 100% 7px');
            console.log('signer_name false');
        }
    },
    phone: function(visitor_phone){
        if((visitor_phone.val() == "")){
            console.log('signer_phone false');
            this.flag[1] = false;
            $("#signer_phone_i").css('background','url("'+ cancel +'") no-repeat 100% 7px');
        }else if((visitor_phone.val() != "") && !searchStr(visitor_phone.val())){
            console.log('signer_phone false');
            this.flag[1] = false;
            $("#signer_phone_i").css('background','url("'+ cancel +'") no-repeat 100% 7px');
        }else {
            this.flag[1] = true;
            console.log('signer_phone true');
            $("#signer_phone_i").css('background','url("'+ tick +'") no-repeat 100% 7px');
        }
    },
    wechat:function(signer_wechat){
        if(signer_wechat.val() == ""){
            this.flag[2] = false;
            $("#signer_wechat_i").css('background','url("'+ cancel +'") no-repeat 100% 7px');
            console.log('signer_wechat flase');
        }
        else if (signer_wechat_zz(signer_wechat.val())) {
            this.flag[2] = true;
            $("#signer_wechat_i").css('background','url("'+ tick +'") no-repeat 100% 7px');
            console.log('signer_wechat true');
        }else{
            this.flag[2] = false;
            $("#signer_wechat_i").css('background','url("'+ cancel +'") no-repeat 100% 7px');
            console.log('signer_wechat false');
        }
    },
    idcard: function(visitor_idcard){
        if(visitor_idcard.val() == ""){
            this.flag[3] = false;
            $("#signer_idcart_i").css('background','url("'+ cancel +'") no-repeat 100% 7px');
            console.log('signer_idcart false');
        }
        else if (validateIdCard(visitor_idcard.val())) {
            this.flag[3] = true;
            $("#signer_idcart_i").css('background','url("'+ tick +'") no-repeat 100% 7px');
            console.log('signer_idcart true');
        }else if(checkGATIdCard(visitor_idcard.val())){
            this.flag[3] = true;
            $("#signer_idcart_i").css('background','url("'+ tick +'") no-repeat 100% 7px');
            console.log('signer_idcart true');
        }else{
            this.flag[3] = false;
            $("#signer_idcart_i").css('background','url("'+ cancel +'") no-repeat 100% 7px');
            console.log('signer_idcart false');
        }
    },
    unit: function(signer_unit){
        if(signer_unit.val() != ""){
            this.flag[4] = true;
            $("#signer_unit_i").css('background','url("'+ tick +'") no-repeat 100% 7px');
            console.log('signer_unit true');
        }
        else{
            this.flag[4] = false;
            $("#signer_unit_i").css('background','url("'+ cancel +'") no-repeat 100% 7px');
            console.log('signer_unit false');
        }
    },
    address: function(signer_address){
        if(signer_address.val() != ""){
            this.flag[5] = true;
            $("#signer_address_i").css('background','url("'+ tick +'") no-repeat 100% 7px');
            console.log('signer_address true');
        }
        else{
            this.flag[5] = false;
            $("#signer_address_i").css('background','url("'+ cancel +'") no-repeat 100% 7px');
            console.log('signer_address false');
        }
    },
    submit: function(){
        if ($.inArray(false,this.flag) == -1) {
            $('#poetrysubmit').submit();
        }else{
            // openModal('#commonModal', '<center>请完善你的信息后提交</center>', false);
            // $('#poetrysubmit').submit();

            swal("请完善您的信息");
        }
    }
}

function validateIdCard(idCard) {
    //15位和18位身份证号码的正则表达式
    var regIdCard = /^(^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$)|(^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[Xx])$)$/;

    //如果通过该验证，说明身份证格式正确，但准确性还需计算
    if (regIdCard.test(idCard)) {
        if (idCard.length == 18) {
            var idCardWi = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2); //将前17位加权因子保存在数组里
            var idCardY = new Array(1, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2); //这是除以11后，可能产生的11位余数、验证码，也保存成数组
            var idCardWiSum = 0; //用来保存前17位各自乖以加权因子后的总和
            for (var i = 0; i < 17; i++) {
                idCardWiSum += idCard.substring(i, i + 1) * idCardWi[i];
            }

            var idCardMod = idCardWiSum % 11; //计算出校验码所在数组的位置
            var idCardLast = idCard.substring(17); //得到最后一位身份证号码

            //如果等于2，则说明校验码是10，身份证号码最后一位应该是X
            if (idCardMod == 2) {
                if (idCardLast == "X" || idCardLast == "x") {
                    return true;
                } else {
                    return false;
                }
            } else {
                //用计算出的验证码与最后一位身份证号码匹配，如果一致，说明通过，否则是无效的身份证号码
                if (idCardLast == idCardY[idCardMod]) {
                    return true;
                } else {
                    return false;
                }
            }
        } else {
            return true;
        }
    } else {
        return false;
    }
}
function searchStr(phoneNum) {
    var search_str = /^1[3|4|5|8][0-9]\d{8}$/;
    return search_str.test(phoneNum);
}
function checkGATIdCard(obj){
    var reg1 = /^[A-Z]{1,2}[0-9]{6}[|\(]?[0-9A-Z][|\)]?$/;//香港格式1 (香港身份证号码结构：XYabcdef(z))  
    var reg2 = /^[A-Z][0-9]{7,12}$/;//香港格式2 (H60152555)  

    var reg3 = /^[1|5|7][0-9]{6}[|\(]?[0-9A-Z][|\)]?$/;//澳门,8位数,不包含出生年月 格式为 xxxxxxx(x) 注:x全为数字,无英文字母 首位数只有1、5、7字开头的  
    var reg4 = /^[a-zA-Z][0-9]{9}$/;//台湾:10位字母和数字  

    if(reg1.test(obj) || reg2.test(obj) || reg3.test(obj) || reg4.test(obj)){
        return true;
    }else{
        return false;
    }
}

function signer_wechat_zz(i){
    var reg=/^[a-zA-Z\d_]{5,}$/;
    if(reg.test(i)){
        return true;
    }else{
        return false;
    }
    
    
}